package dao.impl;
import java.util.ArrayList;

import dao.IDVDDao;
import entity.DVD;

public class DVDDaoImpl implements IDVDDao{
	private ArrayList<DVD> arr = new ArrayList<DVD>();
	
	@Override
	public boolean save(DVD dvd) {
		if(-1 == arr.indexOf(dvd)){
			return arr.add(dvd);
		}
		return false;
	}

	@Override
	public boolean delete(DVD dvd) {
		if(arr.indexOf(dvd) != -1){
			return arr.remove(dvd);
		}
		return false;
	}

	@Override
	public boolean update(DVD dvd) {
		return false;
	}

	@Override
	public DVD selectByName(String name) {
		for (DVD dvd : arr) {
			if(dvd.getDvdName().equals(name)){
				return dvd;
			}
		}
		return null;
	}

	@Override
	public ArrayList<DVD> selectByType(String type) {
		ArrayList<DVD> newArr = new ArrayList<DVD>();
		for (DVD dvd : arr) {
			if(dvd.getDvdType().equals(type)){
				newArr.add(dvd);
			}
		}
		return newArr;
	}

	@Override
	public ArrayList<DVD> selectAll() {
		return arr;
	}

	@Override
	public boolean rentDVD(String name) {
		for (DVD dvd : arr) {
			if(dvd.getDvdName().equals(name) && !dvd.isRent()){
				dvd.setTime(dvd.getTime()+1);
				dvd.setRent(true);
				return true;
			}
		}
		return false;
	}

	@Override
	public boolean returnDVD(String name) {
		for (DVD dvd : arr) {
			if(dvd.getDvdName().equals(name) && dvd.isRent()){
				dvd.setRent(false);
				return true;
			}
		}
		return false;
	}

	@Override
	public ArrayList<DVD> selectAllNotRend() {
		ArrayList<DVD> newArr = new ArrayList<DVD>();
		for (DVD dvd : arr) {
			if(!dvd.isRent()){
				newArr.add(dvd);
			}
		}
		return newArr;
	}

	@Override
	public ArrayList<DVD> descendingOrderByTime() {
		ArrayList<DVD> copy = new ArrayList<DVD>(arr);
		copy.sort((s1,s2) -> s2.getTime() - s1.getTime());
		return copy;
	}

}
